//
//  MainTabBarcontroller.swift
//  HJWeibo
//
//  Created by huangjiong on 16/1/31.
//  Copyright © 2016年 huangjiong. All rights reserved.
//

import UIKit

class MainTabBarcontroller: UITabBarController
{

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        
        //tabBar颜色
        // 注意: 在iOS7以前如果设置了tintColor只有文字会变, 而图片不会变
        tabBar.tintColor = UIColor.orangeColor()
        
        //添加所有子控制器
        addAllChildViewControllers()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置按钮frame
        setupComposeBtn()
    }
    
    
    //设置按钮frame
    private func setupComposeBtn()
    {
        //1.先添加到tabBar上
        tabBar.addSubview(composeBtn)
        
        //设置composeBtn的frame
        let btnWidth = UIScreen.mainScreen().bounds.size.width / CGFloat(viewControllers!.count)
        let btnFrame = CGRect(x: 0, y: 0, width: btnWidth, height: 49)
        composeBtn.frame = CGRectOffset(btnFrame, 2 * btnWidth, 0)
    }
    
    //添加所有子控制器
    private func addAllChildViewControllers()
    {
        //获取json文件,如果是实际开发中,从服务器获取
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType:nil)
        //通过路径创建NSData
        //注意,动态绑定,保证有数据
        if let jsonPath = path{
            let jsonData = NSData(contentsOfFile: jsonPath)
            
            //异常处理
            do {
                //json解析
                let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                for vcDict in dictArr as! [[String: String]]
                {
                    addChildViewController(vcDict["vcName"]!, title: vcDict["title"]!, imageName: vcDict["imageName"]!)
                }
                
            } catch
            {
                //捕获到异常时进来
                //从本地直接创建
                //首页
                addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
                //消息
                addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
                // 再添加一个占位控制器
                addChildViewController("NullViewController", title: "", imageName: "")
                //广场
                addChildViewController("DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
                //我
                addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
                
                
            }
            
        }

    }
    
    //添加子控制器
    private func addChildViewController(childControllerName: String, title:String, imageName:String) {
        
        //1.先动态获取命名空间,从info.plist中获取
        //从info.plist文件中获取命名空间
        //第一个!表示字典有值,第二个!表示指定键有值,再将值转为String类型
        let nameSpace = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        
        //2.将字符串转成类
        //2.1先转成任意类
        let aClass:AnyClass = NSClassFromString(nameSpace + "." + childControllerName)!
        //2.2再转成指定的类型
        let vcClass = aClass as! UIViewController.Type
        
        //3.利用得到的类,来创建对象(控制器)
        let vc = vcClass.init()
        
        
        //4.设置控制器的信息
        //标题
        vc.title = title
        //tabBarItem数据
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        //给首页包装一个导航控制器
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        
        //添加子控制器
        addChildViewController(nav)
        
    }
    
    //监听按钮点击
    func composeBtnClick()
    {
        print("点击了")
    }
    
    
    //MARK: - 懒加载
    private lazy var composeBtn:UIButton = {
        
        let btn = UIButton()
        
        //设置前景图片
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
      btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //添加监听事件
        btn.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()

}
